package com.sgb.kjwl.view.widget.pick;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.PickBiddinglistDetialViewBinding;
import com.sgb.kjwl.model.entity.pick.TenderDictEntity3;
import com.sgb.kjwl.view.ui.activity.pick.PickTenderDetailListActivity;
import com.sgb.kjwl.view.ui.activity.pick.PickTenderInquiryActivity;
import com.sgb.kjwl.view.ui.adapter.pick.PickDetailFileAdapter;

import java.util.ArrayList;
import java.util.List;


/**
  * @Description:
  * @Author:         张驰
  * @CreateDate:     006
  * @UpdateUser:     张驰
  * @UpdateDate:     2020年09月24
  * @UpdateRemark:   报名详情页中招标清单模块布局
  * @Version:        招采 2.7.2
 */
public class PickBiddinglistDetaiView extends RelativeLayout {

    protected Context mContext;
    protected PickBiddinglistDetialViewBinding viewBinding;
    private List<List<TenderDictEntity3>> newTenderList = new ArrayList<>();
    private PickDetailFileAdapter pickDetailFileAdapter;
    private String title;

    public PickBiddinglistDetaiView(Context context) {
        super(context);
        this.mContext = context;
        initView();
    }

    public PickBiddinglistDetaiView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        this.mContext = context;
        initView();
    }

    public PickBiddinglistDetaiView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.mContext = context;
        initView();
    }

    private void initView() {
        viewBinding = DataBindingUtil.inflate(LayoutInflater.from(mContext), R.layout.pick_biddinglist_detial_view, this, true);
    }

    public void showInfoView(List<List<TenderDictEntity3>> tenderList,boolean ifemptyView,int tenderType,String path,List<TenderDictEntity3> filesList,boolean isMore,String tenderId,String title) {
        if (ifemptyView){
            if (tenderList == null) {
//                viewBinding.emptyView.setVisibility(View.VISIBLE);
                return;
            }
            if (tenderList.size() == 0) {
//                viewBinding.emptyView.setVisibility(View.VISIBLE);
                return;
            }
        }else{
            viewBinding.emptyView.setVisibility(View.GONE);
        }
        this.title = title;
        /**
         * 清单附件
        * */
        if (filesList == null || filesList.size() == 0) {
            viewBinding.rootLayoutFile.setVisibility(View.GONE);
        } else {
            viewBinding.rootLayoutFile.setVisibility(View.VISIBLE);
            if (pickDetailFileAdapter == null) {
                pickDetailFileAdapter = new PickDetailFileAdapter(mContext, filesList,title);
            }
            LinearLayoutManager layoutManager = new LinearLayoutManager(mContext);
            viewBinding.pickRecycler.setLayoutManager(layoutManager);
            viewBinding.pickRecycler.setAdapter(pickDetailFileAdapter);
            viewBinding.pickRecycler.setLoadingMoreEnabled(false);
            viewBinding.pickRecycler.setPullRefreshEnabled(false);
        }

        if (tenderType == 6 || tenderType == 7 || tenderType == 8 || tenderType == 9 || tenderType == 10) {
            viewBinding.fileDown.setVisibility(View.GONE);
        } else {
            if ("中标清单".equals(viewBinding.titleNameTv.getText())) {
                viewBinding.fileDown.setVisibility(View.GONE);
            } else {
                viewBinding.fileDown.setVisibility(View.VISIBLE);
                viewBinding.fileDown.setOnClickListener(new OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        //调用系统浏览器下载
                        Uri uri = Uri.parse(path);
                        Intent intent = new Intent(Intent.ACTION_VIEW, uri);
                        mContext.startActivity(intent);
                    }
                });
            }
        }
        viewBinding.rootLayout.removeAllViews();
        if("中标清单".equals(viewBinding.titleNameTv.getText())){
            if (tenderList.size() > 30) {
                viewBinding.tvMore.setVisibility(View.VISIBLE);
                newTenderList.addAll(tenderList.subList(0, 30));
            } else {
                viewBinding.tvMore.setVisibility(View.GONE);
                newTenderList.addAll(tenderList);
            }
        }else{
            if (isMore) {
                viewBinding.tvMore.setVisibility(View.VISIBLE);
                viewBinding.tvMore.setOnClickListener(new OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        PickTenderDetailListActivity.start(mContext, tenderId);
                    }
                });
            } else {
                viewBinding.tvMore.setVisibility(View.GONE);
            }
            newTenderList.addAll(tenderList);
        }
        for (List<TenderDictEntity3> tenderDictEntity : newTenderList) {
            addItemView(tenderDictEntity, viewBinding.rootLayout);
        }
    }

    private void addItemView(List<TenderDictEntity3> tenderDictEntity, LinearLayout linearLayout) {
        PickDetailListView pickDetailListView = new PickDetailListView(mContext);
        pickDetailListView.setRecyclerData(tenderDictEntity,title);
        linearLayout.addView(pickDetailListView);
    }

    public void setText(String title) {
        if ("中标清单".equals(title)) {
            viewBinding.fileDown.setVisibility(View.GONE);
        } else {
            viewBinding.fileDown.setVisibility(View.VISIBLE);
        }
        viewBinding.titleNameTv.setText(title);
    }
}
